﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using yncTech.Common;
using yncTech.BusinessObjects;
using System.Globalization;
using System.Text;
using System.Data.OleDb;
using System.Collections;

using EntitySpaces.Interfaces;

public partial class Admin_SAPFin_SAPFin004a : AdminBasePage
{
    decimal[] Id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Id"] != null)
        {
            Id = Request.QueryString["Id"].TrimEnd(',').Split(',').Select(r => decimal.Parse(r)).ToArray();
        }
        if (!IsPostBack)
        {
            if (Id.Length > 0)
            {
                if (Id.Length == 1)
                {
                    FinReceipt finReceipt = new FinReceipt();
                    if (finReceipt.LoadByPrimaryKey(Id[0]))
                    {
                        if (finReceipt.BoxID.HasValue && finReceipt.BoxID.Value > 0)
                        {
                            //lbMessage.Text = "此凭证还在凭证盒中！";
                            txtReference.ReadOnly = true;
                            txtReference.ForeColor = System.Drawing.Color.Gray;
                            txtDocumentHeaderText.ReadOnly = true;
                            txtDocumentHeaderText.ForeColor = System.Drawing.Color.Gray;
                        }
                        txtPostingDate.Value = finReceipt.PostingDate.HasValue ? finReceipt.PostingDate.Value.ToString("yyyy-MM-dd") : "";
                        txtDocumentsNo.Text = finReceipt.DocumentsNo;
                        txtReference.Text = finReceipt.Reference;
                        txtReferenceKey.Text = finReceipt.ReferenceKey;
                        txtDocumentHeaderText.Text = finReceipt.DocumentHeaderText;
                        drpCompany.SelectedValue = finReceipt.CompanyName + "(" + finReceipt.CompanyCode + ")";
                        if (finReceipt.StorageLocation.HasValue)
                        {
                            BFThirdUnitCabinCollection bFThirdUnitCabinCollection = new BFThirdUnitCabinCollection();
                            bFThirdUnitCabinCollection.Query.Where(bFThirdUnitCabinCollection.Query.ThirdUnitID == finReceipt.StorageLocation.Value)
                                .OrderBy(bFThirdUnitCabinCollection.Query.Name.Ascending);
                            bFThirdUnitCabinCollection.Query.Load();
                            if (finReceipt.ThirdUnitCabinId.HasValue)
                            {
                                BFThirdUnitNumberCollection bFThirdUnitNumberCollection = new BFThirdUnitNumberCollection();
                                bFThirdUnitNumberCollection.Query.Load();
                            }

                            txtRevesedBy.Text = finReceipt.RevesedBy;
                            txtUserName.Text = finReceipt.UserName;
                            txtDocumentDate.Value = finReceipt.DocumentDate.HasValue ? finReceipt.DocumentDate.Value.ToString("yyyy-MM-dd") : "";
                            txtEntryDate.Value = finReceipt.EntryDate.HasValue ? finReceipt.EntryDate.Value.ToString("yyyy-MM-dd") : "";
                            txtLogicalSystem.Text = finReceipt.LogicalSystem;
                            txtDocumentSystem.Text = finReceipt.DocumentSystem;
                            txtDocumentsType.Text = finReceipt.DocumentsType;
                            txtReferenceTransact.Text = finReceipt.ReferenceTransact;
                        }
                    }
                }
                else
                {
                    btnSave.Text = "批量修改";
                }
            }


        }
    }



    protected void btnSave_Click(object sender, EventArgs e)
    {
        FinReceiptCollection finrcieptcollection = new FinReceiptCollection();
        finrcieptcollection.Query.Where(finrcieptcollection.Query.ReceiptID.In(Id));
        finrcieptcollection.Query.Load();
        using (esTransactionScope tr = new esTransactionScope())
        {
            foreach (var receipt in finrcieptcollection)
            {
                string content = string.Empty;
                if (Id.Length > 0)
                {
                    if (receipt.StorageStatus == "S1")
                    {
                        if (receipt.Reference != txtReference.Text)
                        {
                            if (string.IsNullOrEmpty(receipt.Reference))
                            {
                                content += "凭证索引号2(SAP)从未填写改成为" + txtReference.Text + "\r\n";
                            }
                            else
                            {
                                content += "凭证索引号2(SAP)从" + receipt.Reference + "改成为" + txtReference.Text + "\r\n";
                            }
                            receipt.UpdateCount = 1;
                        }
                        receipt.Reference = txtReference.Text;

                        if (receipt.DocumentHeaderText != txtDocumentHeaderText.Text)
                        {
                            if (string.IsNullOrEmpty(receipt.DocumentHeaderText))
                            {
                                content += "凭证索引号4(SAP)从未填写改成为" + txtDocumentHeaderText.Text + "\r\n";
                            }
                            else
                            {
                                content += "凭证索引号4(SAP)从" + receipt.DocumentHeaderText + "改成为" + txtDocumentHeaderText.Text + "\r\n";
                            }
                            receipt.UpdateCount = 1;
                        }
                        receipt.DocumentHeaderText = txtDocumentHeaderText.Text;
                    }
                    if (receipt.UserName != txtUserName.Text)
                    {
                        if (string.IsNullOrEmpty(receipt.UserName))
                        {
                            content += "用户名(SAP)从未填写改成为" + txtUserName.Text + "\r\n";
                        }
                        else
                        {
                            content += "用户名(SAP)从" + receipt.UserName + "改成为" + txtUserName.Text + "\r\n";
                        }
                        receipt.UpdateCount = 1;
                    }
                    receipt.UserName = txtUserName.Text;



                    if (Business.IsDateTime(txtDocumentDate.Value))
                    {
                        if (receipt.DocumentDate != DateTime.Parse(txtDocumentDate.Value))
                        {
                            if (!receipt.DocumentDate.HasValue)
                            {
                                content += "凭证日期(SAP)从未填写改成为" + txtDocumentDate.Value + "\r\n";
                            }
                            else
                            {
                                content += "凭证日期(SAP)从" + receipt.DocumentDate.Value.ToString("yyyy-MM-dd") + "改成为" + txtDocumentDate.Value + "\r\n";
                            }
                            receipt.UpdateCount = 1;
                        }


                        receipt.DocumentDate = DateTime.Parse(txtDocumentDate.Value);
                    }

                    if (receipt.DocumentSystem != txtDocumentSystem.Text)
                    {
                        if (string.IsNullOrEmpty(receipt.DocumentSystem))
                        {
                            content += "凭证系统(SAP)从未填写改成为" + txtDocumentSystem.Text + "\r\n";
                        }
                        else
                        {
                            content += "凭证系统(SAP)从" + receipt.DocumentSystem + "改成为" + txtDocumentSystem.Text + "\r\n";
                        }
                        receipt.UpdateCount = 1;
                    }
                    receipt.DocumentSystem = txtDocumentSystem.Text.Trim();

                    if (receipt.ReferenceTransact != txtReferenceTransact.Text)
                    {
                        if (string.IsNullOrEmpty(receipt.ReferenceTransact))
                        {
                            content += "业务类型(SAP)从未填写改成为" + txtReferenceTransact.Text + "\r\n";
                        }
                        else
                        {
                            content += "业务类型(SAP)从" + receipt.ReferenceTransact + "改成为" + txtReferenceTransact.Text + "\r\n";
                        }
                        receipt.UpdateCount = 1;
                    }
                    receipt.ReferenceTransact = txtReferenceTransact.Text.Trim();

                    if (receipt.DocumentsType != txtDocumentsType.Text)
                    {
                        if (string.IsNullOrEmpty(receipt.DocumentsType))
                        {
                            content += "凭证系统(SAP)从未填写改成为" + txtDocumentsType.Text + "\r\n";
                        }
                        else
                        {
                            content += "凭证系统(SAP)从" + receipt.DocumentsType + "改成为" + txtDocumentsType.Text + "\r\n";
                        }
                        receipt.UpdateCount = 1;
                    }
                    receipt.DocumentsType = txtDocumentsType.Text.Trim();

                    if (receipt.LogicalSystem != txtLogicalSystem.Text)
                    {
                        if (string.IsNullOrEmpty(receipt.LogicalSystem))
                        {
                            content += "关联系统(SAP)从未填写改成为" + txtLogicalSystem.Text + "\r\n";
                        }
                        else
                        {
                            content += "关联系统(SAP)从" + receipt.LogicalSystem + "改成为" + txtLogicalSystem.Text + "\r\n";
                        }
                        receipt.UpdateCount = 1;
                    }
                    receipt.LogicalSystem = txtLogicalSystem.Text.Trim();

                    if (receipt.RevesedBy != txtRevesedBy.Text)
                    {
                        if (string.IsNullOrEmpty(receipt.RevesedBy))
                        {
                            content += "冲销凭证号(SAP)从未填写改成为" + txtRevesedBy.Text + "\r\n";
                        }
                        else
                        {
                            content += "冲销凭证号(SAP)从" + receipt.RevesedBy + "改成为" + txtRevesedBy.Text + "\r\n";
                        }
                        receipt.UpdateCount = 1;
                    }
                    receipt.RevesedBy = txtRevesedBy.Text.Trim();


                    receipt.IsConfirmed = false;
                    receipt.ConfirmedBy = null;
                    receipt.ConfirmedDttm = null;
                    receipt.UpdatedBy = LoginUserInfo.CurrentUser.UserName;
                    receipt.UpdatedDttm = DateTime.Now;
                    receipt.Save();

                    FinReceiptHis finReceiptHis = new FinReceiptHis();
                    finReceiptHis.Content = content;
                    finReceiptHis.ReceiptId = receipt.ReceiptID;
                    finReceiptHis.CreateUser = LoginUserInfo.CurrentUser.LoginName;
                    finReceiptHis.CreateDate = DateTime.Now;
                    finReceiptHis.Save();
                }
            }
            tr.Complete();
        }
        WebHelper.CloseDialog(true);

    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        WebHelper.CloseDialog(true);
    }

}